 <?php
class Bbling_Model_Entity_Financialplan extends Zend_Db_Table_Abstract
{
    protected $_name = 'financial';
 
     /**
     * Adds user to database, sends confirmation email
     *
     * @param string $username
     * @param string $password
     * @param string $email
     * @throws Zend_Db_Statement_Exception
     * @return int The id of the new user
     */
	 public function changetable($tablename)
	 {
		$this->_name=$tablename; 
	 }
	 
	public function create_business_record($userid,$financeid,$marketingid,$companyid,$productid)
    {
		$this->_name='business';
		$data =array();
		$data['user_id']= $userid;
		$data['company_id'] = $companyid;
		$data['product_id']= $productid ;
		$data['marketing_id']= $marketingid;
		$data['financial_id']= $financeid;
		$data['published']= '0';
		$this->insert($data);
	}
	
	
	public function save_marketing()
    {
		$this->_name='marketing';
		$data =array();
		$data['market_overview']='';
		$this->insert($data);
		$id = $this->_db->lastInsertId();				
		return $id;
	}
	public function save_company()
    {
		$this->_name='company';
		$data =array();
		$data['company_name']='';
		$this->insert($data);
		$id = $this->_db->lastInsertId();				
		return $id;
	}
	public function save_product() //already existing table
    {
		$this->_name='product';
		$data =array();
		$data['product_name']='';
		$this->insert($data);
		$id = $this->_db->lastInsertId();				
		return $id;
	}
	
	
	
    public function save_financial($startmonth, $startbalance)
    {
		$data= array();
		$this->_name='financial';
		$data['financial_start_month']= $startmonth;
		$data['starting_balance'] = $startbalance;
		$this->insert($data);
		// now fetch the id of the row you just created and return it
		$id = $this->_db->lastInsertId();				
		return $id;
	}
	
	function business_check_record($userid) //check wether record already exists for the current user in business table?
	{
		$this->_name='business';
		
		//$rowset = $this->fetchAll("user_id ='$userid'");
		//$row = $this->fetchRow($this->select()->where('user_id = ?',$userid));
		$rowset = $this->fetchAll($this->select()->where('user_id = ?',$userid));
		
	    $rowCount = count($rowset);
		if ($rowCount > 0) 
		{		
			$row = $rowset->current();
			return $row->financial_id;			
		} 
		else 
		{		
			return false;		
		}
	}
	
	
	
	
	function updateByFinancialId($data,$id)
	{
		$where = $this->getAdapter()->quoteInto('id = ?', $id);
		$this->update($data,$where);
	}
	
	function deleteByFinancialId($finance_id)
	{
		$where = $this->getAdapter()->quoteInto('financial_id = ?', $finance_id);
		$this->delete($where);
	}
	
	function insertNewRecord($data)
	{
		$this->insert($data);
	}
	function fatchbusinessrecord($userid)
	{
		$this->_name='business';
		$row = $this->fetchRow($this->select()->where('user_id = ?',$userid));
		$rowarray = $row->toArray();
		return $rowarray;
	}
	
	function fetchProductsByFinanceId($finance_id)
	{
		$this->_name='product_financial';
		$rows = $this->fetchAll($this->select()->where('financial_id = ?',$finance_id));
		return $rows->toArray();
	}
	
	function fetchExpensesByFinanceId($finance_id)
	{
		$this->_name='financial_monthly_expenses';
		$rows = $this->fetchAll($this->select()->where('financial_id = ?',$finance_id));
		return $rows->toArray();
	}		
	
	function fetchOocsByFinanceId($finance_id)
	{
		$this->_name='off_cost';
		$rows = $this->fetchAll($this->select()->where('financial_id = ?',$finance_id));
		return $rows->toArray();
	}	
}
?>